package com.ky.h5house;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.ky.h5house.broadcastreceiver.NetStateChangeReceiver;
import com.ky.h5house.broadcastreceiver.net.NetStateChangeObserver;
import com.ky.h5house.broadcastreceiver.net.NetworkType;
import com.ky.h5house.broadcastreceiver.net.NetworkUtil;
import com.ky.h5house.databinding.ActivitySplashBinding;

public class SplashActivity extends AppCompatActivity implements NetStateChangeObserver {

    ActivitySplashBinding mBinding;

    /**
     * 是否联网
     */
    private boolean isNetworkConnected;

    private static final int UPDATE_TIP = 1;

    private int mTipSeq;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivitySplashBinding.inflate(LayoutInflater.from(this));
        setContentView(mBinding.getRoot());
        NetStateChangeReceiver.registerObserver(this);
        NetStateChangeReceiver.registerReceiver(this);
        if (NetworkUtil.isNetworkAvailable(this)) {
            isNetworkConnected = true;
            toMain();
        } else {
            mHandler.sendEmptyMessageDelayed(UPDATE_TIP, 1000);
            mTipSeq = 0;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        NetStateChangeReceiver.unRegisterObserver(this);
        NetStateChangeReceiver.unRegisterReceiver(this);
    }

    @Override
    public void onNetDisconnected() {
    }

    @Override
    public void onNetConnected(NetworkType networkType) {
        if (!isNetworkConnected) {
            mHandler.removeMessages(UPDATE_TIP);
            mBinding.tvTip.setText("联网成功");
            toMain();
        }
    }

    private void toMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_TIP: {
                    if (mTipSeq == 0) {
                        mBinding.tvTip.setText("正在初始化...");
                        mTipSeq++;
                    } else if (mTipSeq > 0) {
                        mBinding.tvTip.setText("正在联网...");
                        mTipSeq++;
                        if (mTipSeq > 30) {
                            Toast.makeText(SplashActivity.this, "联网超时", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                    mHandler.sendEmptyMessageDelayed(UPDATE_TIP, 1000);
                }
            }
        }
    };

}